//
//  JSONtestViewController.m
//  JSONtest
//
//  Created by Hilary Muludiang on 2/18/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "JSONtestViewController.h"
#import "SBJson.h"

@implementation JSONtestViewController

@synthesize capitalCity;
@synthesize population;
@synthesize runJSONCall;
@synthesize JSONresults;
@synthesize cap;

-(IBAction)getCountryInfo:(id)sender{
	// [self searchGeonamesCountry:@"US"];
	[self jsoncall];
	//NSLog(@"tapped!");
}

-(void)jsoncall{
	NSLog(@"Starting jsoncall");
	NSString *JSONstring = [[NSString alloc] init];
	JSONstring =  @"{\"names\":[{\"name\":\"a1\",\"id\":1},{\"name\":\"b2\",\"id\":2}]}";
	NSLog(@"JSONString: %@", JSONstring);
	
	NSDictionary *dictionary = [JSONstring JSONValue];
	NSLog(@"Dictionary value for \"names\" is \"%@\"", [dictionary objectForKey:@"names"]);
	
	// Create array from 'dictionary'
	NSArray *namesArray = [dictionary objectForKey:@"names"];
	NSLog(@"Array is : %@", namesArray);
	
	for (NSDictionary *theArray in namesArray){
		NSLog(@"Student name: %@", [theArray objectForKey:@"name"]);
	
	
	}
	}

-(void)searchGeonamesCountry:(NSString *)countryName
{
	// Build the string to call the Geonames API
	NSString *const userName = @"hlmuludiang";
	 NSString *urlString = [NSString stringWithFormat: @"http://api.geonames.org/countryInfoJSON?formatted=true&country=%@&@&username=%@&style=full", countryName, userName];
	// NSString *urlString = [NSString stringWithFormat: @"http://api.geonames.org/countryInfoJSON?formatted=true&@&username=%@&style=full", userName];

	NSLog(@"urlString: %@", urlString);
	
	// Create NSURL string from formatted string
	NSURL *url = [NSURL URLWithString:urlString];
	NSLog(@"url : %@", url);
	// Setup and start async download
	NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
	NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
	[connection release];
	[request release];
	
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
	// Store incoming data into a string
	NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	NSLog(@"jsonString: %@", jsonString);
	
	// Create a dictionary from the JSON string
	NSDictionary *results = [jsonString JSONValue];
	
	// Build an array from the dictionary for easy access to each entry
	NSArray *geonames = [results objectForKey:@"geonames"];
	NSLog(@"geonames array: %@", geonames);
	
	
	for (NSDictionary *country in geonames)
	{
		// Get title of the image
		NSString *capital = [country objectForKey:@"capital"];
		
		// Save the title to the photo titles array
		[capitalCities addObject:(capital.length > 0 ? capital : @"Untitled")];
		
		// Build the URL to where the image is stored (see the Flickr API)
		// In the format http://farmX.static.flickr.com/server/id_secret.jpg
		// Notice the "_s" which requests a "small" image 75 x 75 pixels
	    NSLog(@"Capital city: %@", capital);
		
	}
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
